<?php

/**
 * This is the model class for table "tbl_teacher".
 *
 * The followings are the available columns in table 'tbl_teacher':
 * @property integer $teacher_id
 * @property integer $user_id
 * @property string $teacher_name
 * @property string $sex
 * @property string $birthday
 * @property string $nation
 * @property string $technical_position
 * @property string $degree
 * @property string $graduated
 * @property string $address
 * @property integer $zip_code
 * @property integer $tel
 * @property string $brief
 * @property string $research_experience
 * @property string $research_achievement
 * @property integer $display_level
 * @property integer $sort
 * @property string $create_date
 *
 * The followings are the available model relations:
 * @property TblDisplayLevel $displayLevel
 */
class Teacher extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Teacher the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_teacher';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, display_level, sort', 'numerical', 'integerOnly'=>true),
			array('teacher_name, nation, zip_code, tel', 'length', 'max'=>20),
			array('sex', 'length', 'max'=>10),
			array('technical_position, degree, graduated', 'length', 'max'=>50),
			array('address', 'length', 'max'=>200),
			array('birthday, brief, research_experience, research_achievement', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('teacher_id, user_id, teacher_name, sex, birthday, nation, technical_position, degree, graduated, address, zip_code, tel, brief, research_experience, research_achievement, display_level, sort, create_date', 'safe', 'on'=>'search'),
		);
	}
    public function getLevelList()
    {
        $model = DisplayLevel::model()->findAll();
        $data = array();
        foreach($model as $elem)
        {
            $data[$elem->display_level] = $elem->description;
        }
        return $data;
    }
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'displayLevel' => array(self::BELONGS_TO, 'DisplayLevel', 'display_level'),
                                                                'courses' => array(self::MANY_MANY, 'Course', 'tbl_course_teacher(teacher_id, course_id)',)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'teacher_id' => yii::t('other/teacher','Teacher'),
			'user_id' => yii::t('other/teacher','User'),
			'teacher_name' => yii::t('other/teacher','Teacher Name'),
			'sex' => yii::t('other/teacher','Sex'),
			'birthday' => yii::t('other/teacher','Birthday'),
			'nation' => yii::t('other/teacher','Nation'),
			'technical_position' => yii::t('other/teacher','Technical Position'),
			'degree' => yii::t('other/teacher','Degree'),
			'graduated' => yii::t('other/teacher','Graduated'),
			'address' => yii::t('other/teacher','Address'),
			'zip_code' => yii::t('other/teacher','Zip Code'),
			'tel' => yii::t('other/teacher','Tel'),
			'brief' => yii::t('other/teacher','Brief'),
			'research_experience' => yii::t('other/teacher','Research Experience'),
			'research_achievement' => yii::t('other/teacher','Research Achievement'),
			'display_level' => yii::t('other/teacher','Display Level'),
			'sort' => yii::t('other/teacher','Sort'),
			'create_date' => yii::t('other/teacher','Create Date'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('teacher_id',$this->teacher_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('teacher_name',$this->teacher_name,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('nation',$this->nation,true);
		$criteria->compare('technical_position',$this->technical_position,true);
		$criteria->compare('degree',$this->degree,true);
		$criteria->compare('graduated',$this->graduated,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('zip_code',$this->zip_code);
		$criteria->compare('tel',$this->tel);
		$criteria->compare('brief',$this->brief,true);
		$criteria->compare('research_experience',$this->research_experience,true);
		$criteria->compare('research_achievement',$this->research_achievement,true);
		$criteria->compare('display_level',$this->display_level);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('create_date',$this->create_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
                    public static function getCourse($courses)
                    {
                        foreach ($courses as $course)
                        {
                            $courseUrl .= CHtml::link($course->course_name, array('course/view', 'id'=>$course->course_id)) . "    ";
                        }
                        
                        return $courseUrl;
                    }
}
